package com.wbl.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbl.train.common.business.dto.TrainDto;
import com.wbl.train.common.business.entity.TrainEntity;
import com.wbl.train.business.mapper.TrainMapper;
import com.wbl.train.business.service.TrainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wbl.train.common.business.vo.TrainVo;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.ResultCode;
import com.wbl.train.common.utils.SnowUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车次 服务实现类
 * @author 王博纶
 * @since 2024-02-01
 */
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, TrainEntity> implements TrainService {

    @Override
    public PageVo<TrainEntity> queryTrain(TrainDto trainDto) {
        // 获取分页所需要的参数
        int page = trainDto.getPage();
        int limit = trainDto.getLimit();

        LambdaQueryWrapper<TrainEntity> trainWrapper = new LambdaQueryWrapper<TrainEntity>()
                .orderByDesc(TrainEntity::getId);
        //创建分页对象
        IPage<TrainEntity> trainPage = new Page<>(page,limit);
        //调用分页方法
        IPage<TrainEntity> trainPageData = page(trainPage,trainWrapper);
        // 获取查询出来的数据集合
        List<TrainEntity> records = trainPageData.getRecords();
        // 将数据塞到PageVo类中并返回出去
        return new PageVo<TrainEntity>()
                .setDataList(records)
                .setPageSize(limit).setCurrentPage(page)
                .setTotal((int) trainPageData.getTotal())
                .setPages((int) trainPageData.getPages());
    }

    @Override
    public boolean insertAndUpdateTrain(TrainEntity train) {
        // 获取当前的时间 --- 用于创建和修改的时候的时间填充
        DateTime now = DateTime.now();
        // 设置火车车次的修改时间
        train.setUpdateTime(now);
        // 判断Id是否为空
        if(ObjectUtil.isNotNull(train.getId())){
            // 根据id查询车站信息
            TrainEntity trainEntity = this.getById(train.getId());
            // 当前id对应的无火车车次信息
            if(ObjectUtil.isNull(trainEntity)){
                throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前火车车次不存在");
            }
            // 更新火车车次信息
            return this.updateById(train);
        }
        // 保存之前的唯一键校验
        TrainEntity trainEntity = getTrainByUniqueKey(train.getCode());
        // 查询出的trainEntity不为空，不允许新增
        if(ObjectUtil.isNotEmpty(trainEntity)){
            throw new ApplicationException(ResultCode.DATA_EXIST,"当前火车车次已存在");
        }
        // 给新增的火车车次设置Id，创建时间和修改时间
        train.setId(SnowUtil.getSnowflakeNextId())
                .setCreateTime(now);
        // 新增火车车次
        return this.save(train);
    }

    /**
     * 根据唯一键查询 train 火车信息
     * @param code 火车车次编码
     * @return train 火车信息
     */
    private TrainEntity getTrainByUniqueKey(String code) {
        // 通过code进行车站信息的查询
        LambdaQueryWrapper<TrainEntity> wrapper = new LambdaQueryWrapper<TrainEntity>()
                .eq(TrainEntity::getCode, code);
        return this.getOne(wrapper);
    }

    @Override
    public List<TrainVo> queryAllTrain() {
        List<TrainEntity> trainList = selectAll();
        return BeanUtil.copyToList(trainList, TrainVo.class);
    }

    public List<TrainEntity> selectAll() {
        LambdaQueryWrapper<TrainEntity> trainWrapper = new LambdaQueryWrapper<TrainEntity>()
                // 设置排序 --- 按照code字段进行排序
                .orderByAsc(TrainEntity::getCode);
        return this.list(trainWrapper);
    }
}
